//
//  CPLabel.m
//  ChinaPoem
//
//  Created by 赵俊明 on 2019/3/7.
//  Copyright © 2019 赵俊明. All rights reserved.
//

#import "CPLabel.h"
@implementation CPLabel
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(cp_pan:)];
        [pan setDelegate:self];
        [self addGestureRecognizer:pan];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cp_TapChange:)];
        tap.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tap];
//        self.backgroundColor = [UIColor greenColor];
    }
    return self;
}
- (void)cp_TapChange:(UITapGestureRecognizer *)recognizer
{
    if ([recognizer state] == UIGestureRecognizerStateBegan){
        if (_CPAction) {_CPAction(YES);}
    }
}

- (void)cp_pan:(UIPanGestureRecognizer *)recognizer
{
    UIGestureRecognizerState state = [recognizer state];
    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [recognizer translationInView:recognizer.view];
        [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
        [recognizer setTranslation:CGPointZero inView:recognizer.view];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef ref = UIGraphicsGetCurrentContext();
    CGContextSetAlpha(ref, 0.8);
    CGRect rFill = CGRectMake(0, 0, self.width, self.height-1);
    CGFloat arr[] = {5,5};
    CGContextSetLineDash(ref, 0, arr, 2);
    CGContextSetStrokeColorWithColor(ref, [UIColor clearColor].CGColor);
    CGContextSetLineWidth(ref, 1);
    CGContextStrokeRect(UIGraphicsGetCurrentContext(), rFill);
    NSDictionary *dic = @{NSFontAttributeName:kFont(_fSize), NSForegroundColorAttributeName:[UIColor blackColor]};
    [_CPText drawInRect:CGRectMake(0, 0, self.width, self.height) withAttributes:dic];
}

@end
